$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'course/acstatisticscourse/list',
        datatype: "json",
        colModel: [			
			// { label: 'id', name: 'id', index: 'id', width: 50, key: true },
			{ label: '课程编号', name: 'courseCode', index: 'courseCode', width: 80, formatter: function (cellvalue, options, rowObject) {
				let key = rowObject.courseId==1?'第一课':rowObject.courseId==2?'第二课':rowObject.courseId==3?'第三课':rowObject.courseId==4?'第四课':rowObject.courseId==5?'第五课':rowObject.courseId==6?'第六课': rowObject.courseId==7?'第七课': ''
				return "<span>"+key+"</span>&nbsp;&nbsp;<a href="+'acstatisticscoursedetail.html?courseId='+rowObject.courseId+" style='cursor: pointer;'>跳转统计</a>&nbsp;&nbsp;&nbsp;<a href="+'acstatisticsanswer.html?courseId='+rowObject.courseId+" style='cursor: pointer;'>跳转题目</a>";
				}
	        }, 			
			{ label: '进入课程人数', name: 'enterCourseCount', index: 'enterCourseCount', width: 80 }, 			
			{ label: '完成课程人数', name: 'finishCourseCount', index: 'finishCourseCount', width: 80 }, 			
			{ label: '单课完成率', name: 'singleCourseFinishRate', index: 'singleCourseFinishRate', width: 80,formatter: formatRate}, 			
			{ label: '首次完课平均时长', name: 'finishCourseAvageTime', index: 'finishCourseAvageTime', width: 80 ,formatter:formatterTime}, 			
			{ label: '首次完课中位数时长', name: 'finishCourseMedianTime', index: 'finishCourseMedianTime', width: 80 ,formatter:formatterTime}, 			
			{ label: '重看人数', name: 'reviewCount', index: 'reviewCount', width: 80 }, 			
			// { label: '更新者', name: 'updateBy', index: 'update_by', width: 80 }, 			
			// { label: '创建者', name: 'createBy', index: 'create_by', width: 80 }, 			
			{ label: '创建时间', name: 'createTime', index: 'create_time', width: 80 }			
        ],
		viewrecords: true,
        height: 385,
        rowNum: 50,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        },
    });
});
function getTime(time) {
	let h = parseInt(time / 60 / 60 % 24)
	h = h < 10 ? '0' + h : h
	let m = parseInt(time / 60 % 60)
	 m = m < 10 ? '0' + m : m
	let s = parseInt(time % 60)
	 s = s < 10 ? '0' + s : s
	return [h, m, s]
}
function formatterTime(params) {
	if (params != undefined) {
		let time = getTime(params);
		return `${time[0]}时${time[1]}分${time[2]}秒`
	}else {
		return ''
	}
}
function formatRate(item){
	return item+"%";
}
var vm = new Vue({
	el:'#app',
	data:{
		showList: true,
		title: null,
		acStatisticsCourse: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.acStatisticsCourse = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.acStatisticsCourse.id == null ? "course/acstatisticscourse/save" : "course/acstatisticscourse/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.acStatisticsCourse),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "course/acstatisticscourse/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "course/acstatisticscourse/info/"+id, function(r){
                vm.acStatisticsCourse = r.acStatisticsCourse;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
		,exportExcel: function (event) {
			window.location.href = baseURL +"course/acstatisticscourse/exportExcel";
		}
	}
});